package org.example.rental.util;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class RandomKeyGenerator {

    public static String generatorSessionKey(){
        // 生成一个256位的AES密钥
        KeyGenerator keyGen = null;
        try {
            keyGen = KeyGenerator.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        SecureRandom secureRandom = new SecureRandom();
        keyGen.init(256, secureRandom);
        SecretKey key = keyGen.generateKey();
        byte[] keyBytes = key.getEncoded();

        // 将byte数组转换为Base64字符串输出
        String keyBase64 = java.util.Base64.getEncoder().encodeToString(keyBytes);
        System.out.println("随机生成的AES key（Base64编码）：" + keyBase64);
        return keyBase64;
    }
}